package com.antis.olsl.data.turnover.detail;

import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.antis.olsl.arouterlib.DataRoutePath;
import com.antis.olsl.base.BaseActivity;
import com.antis.olsl.base.utils.LogUtils;
import com.antis.olsl.base.utils.ToastUtil;
import com.antis.olsl.data.R;
import com.antis.olsl.data.adapter.PosTurnoverDetailAdapter;
import com.antis.olsl.data.databinding.ActivityPosTurnoverDetailBinding;
import com.antis.olsl.data.response.GetPosTurnoverDetailResp;
import com.makeramen.roundedimageview.RoundedImageView;
import com.trello.rxlifecycle2.LifecycleTransformer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Route(path = DataRoutePath.ACTIVITY_POS_TURNOVER_DETAIL)
public class PosTurnoverDetailActivity extends BaseActivity<ActivityPosTurnoverDetailBinding> implements PosTurnoverDetailContract.View {

    @Autowired
    String posNumber;
    RoundedImageView mImageAvatar;
    TextView mTextCashier;
    TextView mTextSeller;

    RelativeLayout mLayoutBillingMessage;
    TextView mTextWeChatCollection;
    TextView mTextAlipayCollection;
    TextView mTextUnionPayCollection;
    TextView mTextCouponCollection;
    TextView mTextCashCollection;
    TextView mTextChangeAmount;
    TextView mTextTotalReceivable;
    TextView mTextTotalDiscount;
    TextView mTextTotalReceived;

    private PosTurnoverDetailAdapter mAdapter;
    private List<GetPosTurnoverDetailResp.ContentBean.ProductListBean> mList = new ArrayList<>();
    private PosTurnoverDetailPresenter mPresenter;

    @Override
    protected ActivityPosTurnoverDetailBinding getViewBinding() {
        return ActivityPosTurnoverDetailBinding.inflate(getLayoutInflater(), baseBinding.getRoot(), true);
    }

    @Override
    protected void initPresenter() {
        super.initPresenter();
        mPresenter = new PosTurnoverDetailPresenter();
        mPresenter.takeView(this);
    }

    @Override
    protected void initViews() {
        ARouter.getInstance().inject(this);
        mAdapter = new PosTurnoverDetailAdapter(R.layout.item_pos_turnover, mList);
        viewBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));

        View headerView = getLayoutInflater().inflate(R.layout.layout_pos_turnover_detail_header, viewBinding.recyclerView, false);
        mImageAvatar = headerView.findViewById(R.id.image_avatar);
        mTextCashier = headerView.findViewById(R.id.text_cashier);
        mTextSeller = headerView.findViewById(R.id.text_seller);

        View footerView = getLayoutInflater().inflate(R.layout.layout_pos_turnover_detail_footer, viewBinding.recyclerView, false);
        mLayoutBillingMessage = footerView.findViewById(R.id.layout_billing_message);
        mTextWeChatCollection = footerView.findViewById(R.id.text_weChat_collection);
        mTextAlipayCollection = footerView.findViewById(R.id.text_alipay_collection);
        mTextUnionPayCollection = footerView.findViewById(R.id.text_unionPay_collection);
        mTextCouponCollection = footerView.findViewById(R.id.text_coupon_collection);
        mTextCashCollection = footerView.findViewById(R.id.text_cash_collection);
        mTextChangeAmount = footerView.findViewById(R.id.text_change_amount);
        mTextTotalReceivable = footerView.findViewById(R.id.text_total_receivable);
        mTextTotalDiscount = footerView.findViewById(R.id.text_total_discount);
        mTextTotalReceived = footerView.findViewById(R.id.text_total_received);

        mAdapter.addHeaderView(headerView);
        mAdapter.addFooterView(footerView);

        viewBinding.recyclerView.setAdapter(mAdapter);
    }

    @Override
    protected void initData() throws IllegalAccessException {
        Map<String, Object> map = new HashMap<>(1);
        map.put("posNumber", posNumber);
        mPresenter.getPosTurnoverDetail(map);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != mPresenter) {
            mPresenter.unSubscribe();
        }
    }

    @Override
    public void getPosTurnoverDetailSuccess(GetPosTurnoverDetailResp.ContentBean bean) {
        mList.clear();
        GetPosTurnoverDetailResp.ContentBean.CashRegisterInfoBean cashRegisterInfo = bean.cashRegisterInfo;
        if (null != cashRegisterInfo) {
            mTextCashier.setText(cashRegisterInfo.cashier);
            mTextSeller.setText(cashRegisterInfo.seller);
        }
        if (null != bean.productList) {
            mList.addAll(bean.productList);
        }
        GetPosTurnoverDetailResp.ContentBean.BillingInfoBean billingInfo = bean.billingInfo;
        if (null != billingInfo) {
            mTextWeChatCollection.setText(billingInfo.weChatCollection);
            mTextAlipayCollection.setText(billingInfo.alipayCollection);
            mTextUnionPayCollection.setText(billingInfo.unionPayCollection);
            mTextCouponCollection.setText(billingInfo.couponCollection);
            mTextCashCollection.setText(billingInfo.cashCollection);
            mTextChangeAmount.setText(billingInfo.changeAmount);
            mTextTotalReceivable.setText(billingInfo.totalReceivable);
            mTextTotalDiscount.setText(billingInfo.totalDiscount);
            mTextTotalReceived.setText(billingInfo.totalReceived);
        }
        mAdapter.setList(mList);
    }

    @Override
    public void getPosTurnoverDetailFailure(String msg) {
        LogUtils.e("getPosTurnoverDetailFailure msg= " + msg);
        if (TextUtils.isEmpty(msg)) {
            msg = "请求失败,请稍后重试";
        }
        ToastUtil.showToast(this, msg);
    }

    @Override
    public boolean isActive() {
        return false;
    }

    @Override
    public <T> LifecycleTransformer<T> bindToLife() {
        return this.bindToLifecycle();
    }
}